﻿/// <reference path="jquery-1.3.2-vsdoc.js">
(function () {
    /*
    *Author Kwan
    *Date 2010-11-22
    *Function jBase class
    */

    //check the class name , it will be replaced when existed
    if (window.job) {
        alert('variable jBase has been used,it will be replaced with _Applican!');
        window._Applican = window.job;
    }

    //constructor
    window.Job = function (param) {
        param = param instanceof Object ? param : {};
        this.cfg = param.cfg || {};
    }
    var pt = Job.prototype;

    pt.limit = 20;
    pt.start = 0;
    pt.successCount = 0;
    pt.failCount = 0;

    pt.html = [];
    pt.html['SearchList'] = "<table width='750' border='0' cellspacing='0' cellpadding='0' class='table6'>" +
    "<tr class='XCss'>" +
	"<td width='164' class='tab6Td1'><a href='EditJob.aspx?a=XNumber'>XJobName</a></td>" +
	"<td width='79'>XCity</td>" +
	"<td width='78'><span style='color:#F63'><a href='Applicans.aspx'>XAppCounts</a></span></td>" +
    "<td width='73'><span style='color:#F63'>XIssuedates</span></td>" +
	"<td width='98'>XEndDates</td>" +
	"<td width='83'>XLevel</td> " +
	"<td width='175' align='left'>" +
    "<a href='EditJob.aspx?a=XNumber'>修改</a>&nbsp;&nbsp;" +
    "<a onclick=\"return confirm('你确定要删除吗? ');\" id='Repeater1_DelLink_5' href='javascript:__doPostBack('Repeater1$ctl05$DelLink','')'>删除</a>&nbsp;&nbsp;" +
    "<a id='Repeater1_Renewal_5' href='javascript:__doPostBack('Repeater1$ctl05$Renewal','')'>续期30天</a>" +
    "</td></tr></table>";


    pt.Degree = ['其他', '初中', '高中', '中技', '中专', '大专', '本科', '硕士', '博士', '不限'];
    pt.Workyear = ['不限', '在读学生', '应届毕业生', '一年以上', '两年以上', '三年以上', '五年以上', '八年以上', '十年以上'];

    $(function () {
        $('#searchForm').submit(function () {
            pt.start = 0; pt.limit = 20;
            queryStr = decodeURI($("#searchForm").formSerialize());
            //queryStr = queryStr.replace(/%2C/g, ',');
            var options1 = {
                url: 'facade.axd',
                dataType: "json",
                data: { action: 'Job', start: pt.start, limit: pt.limit, s_method: 'list', query: queryStr },
                beforeSubmit: pt.beforeSubmit,
                success: pt.showResponse
                //complete: function(x) { $("#resultList").html(queryStr + "\r\n" + x.responseText); }
            };
            $('#searchForm').ajaxForm(options1);
            return false;
        });
    });
    pt.beforeSubmit = function (obj) {
        pt.loading();
    }

    pt.loading = function () {
        var div = document.createElement("div");
        div.innerHTML = "<div class='loading'> 数据加载中,请稍后..</div>";
        div.setAttribute("id", "tool_loading");
        div.style.position = "absolute";
        div.style.left = (document.body.clientWidth / 2 - div.clientWidth / 2);
        div.style.top = (document.documentElement.scrollTop + document.body.clientHeight / 2.5);
        document.body.appendChild(div);
    }
    pt.removeLoading = function () {
        $("#tool_loading").remove();
    }
    pt.t1 = '';
    pt.jumpp = function (page) {
        pt.t1 = new Date().getTime();
        queryStr = decodeURI(queryStr);
        pt.start = String(Number(page) * pt.limit - pt.limit);
        var options2 = {
            url: 'facade.axd',
            dataType: "json",
            data: { action: 'Job', start: pt.start, limit: pt.limit, s_method: 'list', query: queryStr },
            beforeSubmit: pt.beforeSubmit,
            success: pt.showResponse
            //complete: function(x) { $("#resultList").html(queryStr + x.responseText); }
        };
        $('#searchForm').ajaxSubmit(options2);
    }
    pt.AppJob = function (rid, jid) {
        var now = new Date();
        var nowDate = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate() + " " + now.toLocaleTimeString();
        $.ajax({ url: "facade.axd", cache: false, dataType: "json", data: encodeURI("action=Applican&s_method=save&data={\"ResumeID\":\"" + rid + "\",\"JobID\":\"" + jid + "\",\"appDate\":\"" + nowDate + "\",\"State\":\"0\"}"),
            beforeSend: function (x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); },
            complete: function (x, state) { pt.removeLoading(); }
        });
    }

    pt.checkJob = function (jid) {
        if (rid == "") {
            if (confirm("您还没有创建简历，现在创建吗？")) { window.location = "/Browser/MyXINJOB.aspx"; }
            return false;
        }
        $.ajax({ url: "facade.axd", cache: false, dataType: "json", data: "action=Applican&s_method=list&query=JobID=" + jid + "%26ResumeID=" + rid + "&limit=2",
            complete: function (x, state) {
                var json = eval("(" + x.responseText + ")");
                if (json.totalCount == 0) { pt.AppJob(rid, jid); pt.successCount++; } else { pt.failCount++; }
                redraw("<li style='text-align:center;'>成功申请 <font color='green'>" + pt.successCount + "</font> 个职位！申请失败 <font color='red'>" + pt.failCount + "</font> 次</li>");
                window.setInterval("FixPosition()", 1);
            }
        });
    }

    pt.appSingleJob = function (j) {
        if (typeof (uid) == "undefined" || uid == "Error" || uid == "") { alert('请登录后再申请职位！'); window.location = "/Login.aspx?returnUrl=%2fAdvanceSearch.aspx"; }
        jid = j.id.replace('Appli_', '')
        this.checkJob(jid);
    }

    pt.applySelectedJobs = function () {
        if (typeof (uid) == "undefined" || uid == "Error" || uid == "") { alert('请登录后再申请职位！'); window.location = "/Login.aspx?returnUrl=%2fAdvanceSearch.aspx"; }
        var jobArray = [];
        var chxs = document.getElementById('resultList').getElementsByTagName('input');
        var j = 0;
        for (var i = 0; i < chxs.length; i++) {
            if (/^check_/.test(chxs[i].name) && chxs[i].checked) {
                jobArray[j] = chxs[i].value;
                j++;
            }
        }
        if (jobArray.length == 0) { alert("请在要选择的职位前打勾！"); return; }
        for (var k = 0; k < jobArray.length; k++) {
            this.checkJob(jobArray[k]);
        }
    }
    pt.showResponse = function (responseText, statusText) {
        $("input[name='checkAll']").attr("checked", false);
        var str = ""; var pstr = ""; var totalCount = responseText.totalCount; var firstPage = 1; var thisPage = 1;
        if (totalCount > 0) {
            var jStr = "";
            var jHtml = pt.html['SearchList'];
            for (var i = 0; i < responseText.data.length; i++) {
                jStr = jHtml;
                jStr = jStr.replace(/XUID/g, uid);
                jStr = jStr.replace(/XNumber/g, responseText.data[i].id);
                jStr = jStr.replace(/XCompanyID/g, responseText.data[i].CompanyID);
                jStr = jStr.replace(/XJobName/g, responseText.data[i].JobName);
                jStr = jStr.replace(/XCity/g, responseText.data[i].City);
                jStr = jStr.replace(/XIssuedate/g, responseText.data[i].Issuedate.replace(/ 00:00:00/g, ""));
                jStr = jStr.replace(/XEducation/g, pt.Degree[responseText.data[i].Degreefrom]);
                jStr = jStr.replace(/XWorkYears/g, pt.Workyear[responseText.data[i].Workyear]);
                jStr = jStr.replace(/XDuty/g, responseText.data[i].Duty);
                jStr = jStr.replace(/XCompanyName/g, responseText.data[i].CompanyName);
                jStr = jStr.replace(/XEnrollments/g, responseText.data[i].Enrollments);
                str += jStr;
            }
            var pageCount = Math.ceil(responseText.totalCount / pt.limit);
            $('#totalCount').html("共有职位 " + responseText.totalCount + " 个");
            if (pageCount > 0) {
                thisPage = pt.start / pt.limit + 1;
                var rightCount = pageCount - thisPage;
                var leftCount = thisPage - 1;
                var leftStr = ""; var rightStr = "";
                var pHtml = "<table  class='searchPageNav'><tr>XLeftXPagesXRight</tr></table>";
                //当前页右边页码
                if (pageCount <= thisPage) {    //当前页为最后一页
                    rightStr = "<td><img src='/Images/pageroff.gif' border='0' /></td>";
                }
                if (pageCount > thisPage) {     //当前页非最后一页
                    var rCount = rightCount > 4 ? 4 : rightCount; //当前页后面的页数
                    for (var r = thisPage + 1; r <= thisPage + rCount; r++) {
                        rightStr += "<td><a href='#" + r + "' class='orange1' onclick=app.jumpp(" + r + ") >" + r + "</a></td>";
                    }
                    rightStr += "<td><a href='#" + (thisPage + 1) + "' class='orange1' onclick='app.jumpp(" + (thisPage + 1) + ");' style='border:0px; width:auto;margin-left:5px;' ><img src='/Images/pageron.gif' border='0' /></a></td>";
                }
                //当前页左边页码
                if (thisPage <= firstPage) {    //当前页为第一页
                    leftStr = "<td style='padding-right:5px;' ><img src='/Images/pageloff.gif' border='0' /></td>";
                }
                if (thisPage > firstPage) {     //当前页非第一页
                    leftStr = "<td><a href='#" + (thisPage - 1) + "' class='orange1' onclick=app.jumpp(" + (thisPage - 1) + ") style='border:0px; width:auto;margin-right:5px;'><img src='/Images/pagelon.gif' border='0' /></a></td>";
                    var lCount = leftCount > 4 ? 4 : leftCount;  //当前页前面的页数
                    var beginPage = thisPage > 4 ? thisPage - 4 : 1;    //开始页码
                    for (var l = beginPage; l <= lCount + beginPage - 1; l++) {
                        leftStr += "<td><a href='#" + l + "' class='orange1' onclick=app.jumpp(" + l + ") >" + l + "</a></td>";
                    }
                }
                //替换代码
                pHtml = pHtml.replace(/XLeft/g, leftStr);
                pHtml = pHtml.replace(/XPages/g, "<td  class='currPage' >" + thisPage + "</td>");
                pHtml = pHtml.replace(/XRight/g, rightStr);
                //写入页面
                $("td[name='nav_page']").html(pHtml);
                //$("#nav_page").html(pHtml);
            }
            $("#resultList").html("<tr class='titleTr'><td  class='td0'>&nbsp;</td><td  class='td1'>职位名称</td><td  class='td2'>公司名称</td><td  class='td3'>工作地点</td><td  class='td4'>发布日期</td><td  class='td5'>&nbsp;</td></tr>" + str);
        }
        else {
            $("td[name='nav_page']").html("");
            $("span[name='totalCount']").html("共有职位 0 个");
            //$("#nav_page").html("");
            //$("#totalCount").html("共有职位 0 个");
            $("#resultList").html("<tr><td>暂无相关职位，请重新搜索！</td></tr>");
        }
        pt.removeLoading();
        //$(".loading").fadeOut();
        window.scrollTo(0, 0);
        //alert((new Date().getTime() - pt.t1) / 1000 + " 秒");
    }


})();

window.job = new Job();
window._ = job._;